﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLCV
{
    public partial class frmTraCuuDen : Form
    {

        CongVanDenSearchDTO _congVanDenSearchDTO = new CongVanDenSearchDTO();
        public frmTraCuuDen()
        {
            InitializeComponent();
        }

        void DateTimePicker_Enable(DateTimePicker dtp)
        {
            dtp.CustomFormat = "d/M/yyyy";
            dtp.Enabled = true;
        }

        void DateTimePicker_Disable(DateTimePicker dtp)
        {
            dtp.CustomFormat = " ";
            dtp.Enabled = false;
        }


        private void frmTraCuuDen_Load(object sender, EventArgs e)
        {
            //theo ngay luu
            rbtnTatCa.Checked = true;
            //an cac nut khac
            ckbDenNgay.Enabled = false;
            ckbDenNgay.Checked = false;
            ckbTuNgay.Enabled = false;
            ckbTuNgay.Checked = false;

            //theo han xu ly
            rbtnTatCa_HXL.Checked = true;
            //an cac nut của phan này
            ckbDenNgay_HXL.Enabled = false;
            ckbDenNgay_HXL.Checked = false;
            ckbTuNgay_HXL.Enabled = false;
            ckbTuNgay_HXL.Checked = false;

            //load hệ thống , đơn vị , loại và người tiếp nhận xử lý
            cmbHeThong.DisplayMember = "Ten";
            cmbHeThong.ValueMember = "ID";
            List<HeThongDTO> tmpHeThong = HeThongBUS.LoadAll();
            tmpHeThong.Insert(0, new HeThongDTO (0, "<Tìm tất cả>"));
            cmbHeThong.DataSource = tmpHeThong;




            List<DonViDTO> tmpDonVi = new List<DonViDTO>();
            tmpDonVi.Insert(0, new DonViDTO(0, "<Tìm tất cả>", null, 0));
            cmbDonVi.DataSource = tmpDonVi;
            cmbDonVi.DisplayMember = "Ten";
            cmbDonVi.ValueMember = "ID";

            List<LoaiDenDTO> tmpLoaiDen= LoaiDenBUS.LoadAll();
            tmpLoaiDen.Insert(0, new LoaiDenDTO(0, "<Tìm tất cả>"));
            cmbLoai.DataSource = tmpLoaiDen;
            cmbLoai.DisplayMember = "Ten";
            cmbLoai.ValueMember = "ID";

            List<NguoiDTO> tmpNguoi = NguoiBUS.LoadAll();
            tmpNguoi.Insert(0, new NguoiDTO(0, "<Tìm tất cả>", false));
            cmbNguoiTiepNhan.DataSource = new List<NguoiDTO>(tmpNguoi);
            cmbNguoiTiepNhan.DisplayMember = "Ten";
            cmbNguoiTiepNhan.ValueMember = "ID";

            cmbNguoiXuLy.DataSource = tmpNguoi;
            cmbNguoiXuLy.DisplayMember = "Ten";
            cmbNguoiXuLy.ValueMember = "ID";

            btnChinhSuaDen.Enabled = false;
            btnXemChiTietDen.Enabled = false;
            btnLuu.Enabled = false;



        }

        private void rbtnTrongNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongNgay.Checked)
                {
                    DateTimePicker_Enable(dtpTrongNgay);

                    _congVanDenSearchDTO.NgayLuuMax = dtpTrongNgay.Value;
                    _congVanDenSearchDTO.NgayLuuMin = dtpTrongNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTrongNgay);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongKhoang_CheckedChanged(object sender, EventArgs e)
        {

            try
            {
                if (rbtnTrongKhoang.Checked)
                {
                    ckbDenNgay.Enabled = true;
                    ckbDenNgay.Checked = true;
                    ckbTuNgay.Enabled = true;
                    ckbTuNgay.Checked = true;

                    _congVanDenSearchDTO.NgayLuuMax = dtpDenNgay.Value;
                    _congVanDenSearchDTO.NgayLuuMin = dtpTuNgay.Value;
                }
                else
                {
                    ckbDenNgay.Enabled = false;
                    ckbDenNgay.Checked = false;
                    ckbTuNgay.Enabled = false;
                    ckbTuNgay.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbTuNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbTuNgay.Checked)
                {
                    DateTimePicker_Enable(dtpTuNgay);
                    if (ckbDenNgay.Checked && dtpTuNgay.Value > dtpDenNgay.Value)
                    {
                        dtpTuNgay.Value = dtpDenNgay.Value;
                    }
                    _congVanDenSearchDTO.NgayLuuMin = dtpTuNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTuNgay);
                    _congVanDenSearchDTO.NgayLuuMin = null;
                    //dtpDenNgay.MinDate = new DateTime(1973, 1, 1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbDenNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbDenNgay.Checked == true)
                {
                    DateTimePicker_Enable(dtpDenNgay);
                    if (ckbTuNgay.Checked && dtpTuNgay.Value > dtpDenNgay.Value)
                    {
                        dtpDenNgay.Value = dtpTuNgay.Value;
                    }
                    _congVanDenSearchDTO.NgayLuuMax = dtpDenNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpDenNgay);
                    _congVanDenSearchDTO.NgayLuuMax = null;
                    //dtpTuNgay.MaxDate = new DateTime(9990, 12, 31);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTatCa_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTatCa.Checked)
                {
                    _congVanDenSearchDTO.NgayLuuMax = null;
                    _congVanDenSearchDTO.NgayLuuMin = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTatCa_HXL_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTatCa_HXL.Checked)
                {
                    _congVanDenSearchDTO.NgayXuLyMin = null;
                    _congVanDenSearchDTO.NgayXuLyMax = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongNgay_HXL_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongNgay_HXL.Checked)
                {
                    DateTimePicker_Enable(dtpTrongNgay_HXL);

                    _congVanDenSearchDTO.NgayXuLyMax = dtpTrongNgay_HXL.Value;
                    _congVanDenSearchDTO.NgayXuLyMin = dtpTrongNgay_HXL.Value;

                }
                else
                {
                    DateTimePicker_Disable(dtpTrongNgay_HXL);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongKhoang_HXL_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongKhoang_HXL.Checked)
                {
                    ckbDenNgay_HXL.Enabled = true;
                    ckbDenNgay_HXL.Checked = true;
                    ckbTuNgay_HXL.Enabled = true;
                    ckbTuNgay_HXL.Checked = true;

                    dtpTrongNgay_HXL.Enabled = false;

                    _congVanDenSearchDTO.NgayXuLyMax = dtpDenNgay_HXL.Value;
                    _congVanDenSearchDTO.NgayXuLyMin = dtpTuNgay_HXL.Value;
                }
                else
                {
                    ckbDenNgay_HXL.Enabled = false;
                    ckbDenNgay_HXL.Checked = false;
                    ckbTuNgay_HXL.Enabled = false;
                    ckbTuNgay_HXL.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbTuNgay_HXL_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbTuNgay_HXL.Checked)
                {
                    DateTimePicker_Enable(dtpTuNgay_HXL);
                    if (ckbDenNgay_HXL.Checked && dtpTuNgay_HXL.Value > dtpDenNgay_HXL.Value)
                    {
                        dtpTuNgay_HXL.Value = dtpDenNgay_HXL.Value;
                    }
                    _congVanDenSearchDTO.NgayXuLyMin = dtpTuNgay_HXL.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTuNgay_HXL);
                    _congVanDenSearchDTO.NgayXuLyMin = null;
                    //dtpDenNgay_HXL.MinDate = new DateTime(1973, 1, 1);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbDenNgay_HXL_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbDenNgay_HXL.Checked == true)
                {
                    DateTimePicker_Enable(dtpDenNgay_HXL);
                    if (ckbTuNgay_HXL.Checked && dtpTuNgay_HXL.Value > dtpDenNgay_HXL.Value)
                    {
                        dtpDenNgay_HXL.Value = dtpTuNgay_HXL.Value;
                    }
                    _congVanDenSearchDTO.NgayXuLyMax = dtpDenNgay_HXL.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpDenNgay_HXL);
                    _congVanDenSearchDTO.NgayXuLyMax = null;
                    //dtpTuNgay_HXL.MaxDate = new DateTime(9990, 12, 31);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThoatDen_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChinhSuaDen_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemDen frm = new frmThemDen();
                if (dataGridView1.SelectedRows.Count != 1)
                {
                    return;
                }
                string tmp = dataGridView1["clnID", dataGridView1.CurrentRow.Index].Value.ToString();
                frm.CongVanDen = CongVanDenBUS.Load(int.Parse(tmp));                
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTraCuuDen_Click(object sender, EventArgs e)
        {
            try
            {
                lblMess.Text = "Đang tìm kiếm...";
                dataGridView1.Refresh();
                dataGridView1.ClearSelection();

                //DataTable kq = new DataTable();
                //kq = CongVanDenBUS.TraCuu(_congVanDenSearchDTO);

                dataGridView1.DataSource = CongVanDenBUS.TraCuu(_congVanDenSearchDTO);
                lblMess.Text = "Tìm thấy " + dataGridView1.RowCount.ToString() + " kết quả.";
                if (dataGridView1.RowCount == 0)
                {
                    btnLuu.Enabled = false;
                }
                else
                {
                    btnLuu.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                String pathname = "";
                SaveFileDialog savediaglog = new SaveFileDialog();
                savediaglog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                savediaglog.Filter = "Excel (*.xls)|*.xls|All Files (*.*)|*.*";
                savediaglog.FilterIndex = 1;
                //savediaglog.OverwritePrompt = true;

                if (savediaglog.ShowDialog() == DialogResult.OK)
                {
                    pathname = savediaglog.FileName;


                    if (pathname != "")
                    {

                        DataTable data = (DataTable)dataGridView1.DataSource;
                        if (data != null)
                        {
                            for (int i = 0; i < data.Columns.Count; i++)
                            {
                                data.Columns[i].Caption = dataGridView1.Columns[i].HeaderText;
                            }
                            data.Columns.RemoveAt(data.Columns.Count-1);
                            CongVanDenBUS.Luu(pathname, data);
                        }
                        else
                            MessageBox.Show("Không Có Dữ Liệu ");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXemChiTietDen_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemDen frm = new frmThemDen();
                if (dataGridView1.SelectedRows.Count != 1)
                {
                    return;
                }
                string tmp = dataGridView1["clnID", dataGridView1.CurrentRow.Index].Value.ToString();
                frm.CongVanDen = CongVanDenBUS.Load(int.Parse(tmp));
                frm.ReadOnly = true;
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dtpTrongNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayLuuMax = dtpTrongNgay.Value;
            _congVanDenSearchDTO.NgayLuuMin = dtpTrongNgay.Value;
        }

        private void dtpTuNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayLuuMin = dtpTuNgay.Value;
            if (dtpDenNgay.Value < dtpTuNgay.Value && ckbDenNgay.Checked)
            {
                dtpDenNgay.Value = dtpTuNgay.Value;
            }
            //dtpDenNgay.MinDate = dtpTuNgay.Value;
        }

        private void dtpDenNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayLuuMax = dtpDenNgay.Value;
            if (dtpDenNgay.Value < dtpTuNgay.Value && ckbTuNgay.Checked)
            {
                dtpTuNgay.Value = dtpDenNgay.Value;
            }
            //dtpTuNgay.MaxDate = dtpDenNgay.Value;
        }

        private void dtpTrongNgay_HXL_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayXuLyMax = dtpTrongNgay_HXL.Value;
            _congVanDenSearchDTO.NgayXuLyMin = dtpTrongNgay_HXL.Value;
        }

        private void dtpTuNgay_HXL_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayXuLyMin = dtpTuNgay_HXL.Value;
            if (dtpDenNgay_HXL.Value < dtpTuNgay_HXL.Value && ckbDenNgay_HXL.Checked)
            {
                dtpDenNgay_HXL.Value = dtpTuNgay_HXL.Value;
            }
            //dtpDenNgay_HXL.MinDate = dtpTuNgay_HXL.Value;
        }

        private void dtpDenNgay_HXL_ValueChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NgayXuLyMax = dtpDenNgay_HXL.Value;
            if (dtpDenNgay_HXL.Value < dtpTuNgay_HXL.Value && ckbTuNgay_HXL.Checked)
            {
                dtpTuNgay_HXL.Value = dtpDenNgay_HXL.Value;
            }
            //dtpTuNgay_HXL.MaxDate = dtpDenNgay_HXL.Value;
        }

        private void cmbHeThong_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<DonViDTO> tmpDonVi;
            if(cmbHeThong.SelectedIndex==-1)
            {
                _congVanDenSearchDTO.HeThong=null;
                tmpDonVi = new List<DonViDTO>();
                tmpDonVi.Insert(0, new DonViDTO(0, "<Tìm tất cả>", null, 0));
                cmbDonVi.DataSource = tmpDonVi;
                cmbDonVi.DisplayMember = "Ten";
                cmbDonVi.ValueMember = "ID";
                cmbDonVi.Enabled = false;
                return;
            }

            HeThongDTO tmp = (HeThongDTO)cmbHeThong.SelectedItem;
            if(tmp.ID==0)
            {
                _congVanDenSearchDTO.HeThong=null;
                tmpDonVi = new List<DonViDTO>();
                tmpDonVi.Insert(0, new DonViDTO(0, "<Tìm tất cả>", null, 0));
                cmbDonVi.DataSource = tmpDonVi;
                cmbDonVi.DisplayMember = "Ten";
                cmbDonVi.ValueMember = "ID";
                cmbDonVi.Enabled = false;
                return;
            }

            _congVanDenSearchDTO.HeThong = tmp;
            tmpDonVi = DonViBUS.LoadAll(tmp.ID, false);
            tmpDonVi.Insert(0, new DonViDTO(0, "<Tìm tất cả>", null, 0));
            cmbDonVi.DataSource = tmpDonVi;
            cmbDonVi.DisplayMember = "Ten";
            cmbDonVi.ValueMember = "ID";
            cmbDonVi.Enabled = true;

        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDonVi.SelectedIndex == -1)
            {
                _congVanDenSearchDTO.DonVi = null;
                return;
            }

            DonViDTO tmp = (DonViDTO)cmbDonVi.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDenSearchDTO.DonVi = null;
                return;
            }
            _congVanDenSearchDTO.DonVi = tmp;
        }

        private void cmbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLoai.SelectedIndex == -1)
            {
                _congVanDenSearchDTO.Loai = null;
                return;
            }

            LoaiDenDTO tmp = (LoaiDenDTO)cmbLoai.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDenSearchDTO.Loai = null;
                return;
            }
            _congVanDenSearchDTO.Loai = tmp;
        }

        private void cmbNguoiTiepNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNguoiTiepNhan.SelectedIndex == -1)
            {
                _congVanDenSearchDTO.NguoiNhan = null;
                return;
            }

            NguoiDTO tmp = (NguoiDTO)cmbNguoiTiepNhan.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDenSearchDTO.NguoiNhan = null;
                return;
            }
            _congVanDenSearchDTO.NguoiNhan = tmp;
        }

        private void cmbNguoiXuLy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNguoiXuLy.SelectedIndex == -1)
            {
                _congVanDenSearchDTO.NguoiXuLy = null;
                return;
            }

            NguoiDTO tmp = (NguoiDTO)cmbNguoiXuLy.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDenSearchDTO.NguoiXuLy = null;
                return;
            }
            _congVanDenSearchDTO.NguoiXuLy = tmp;
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.NoiDung = txtNoiDung.Text;
        }

        private void txtGhiChu_TextChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.GhiChu = txtGhiChu.Text;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && (dataGridView1.CurrentRow.Index >= 0 || dataGridView1.CurrentRow.Index < dataGridView1.RowCount))
            {
                btnXemChiTietDen.Enabled = true;
                btnChinhSuaDen.Enabled = true;
            }
            else
            {
                btnXemChiTietDen.Enabled = false;
                btnChinhSuaDen.Enabled = false;
            }
        }

        private void txtSoKiHieu_TextChanged(object sender, EventArgs e)
        {
            _congVanDenSearchDTO.SoKyHieuVanBan = txtSoKiHieu.Text;
        }
        

    }

}
